package com.yc.sole.system.config;

import com.google.common.collect.Lists;
import com.yc.sole.common.constant.CacheConstant;
import com.yc.sole.common.constant.PubConstant;
import com.yc.sole.framework.redis.RedissonService;
import com.yc.sole.framework.redis.enums.CacheTime;
import com.yc.sole.framework.web.BaseUserInfo;
import com.yc.sole.framework.web.SecurityAuthenticator;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;

/**
 * @author yizuomin
 * @date 2023/3/27 21:36
 **/
@Configuration
@RequiredArgsConstructor
public class SecurityConfig implements SecurityAuthenticator {

    private final RedissonService redissonService;

    @Override
    public void login(String token, Object value) {
        redissonService.setString(CacheConstant.AUTH_TOKEN_CACHE_MAP, token, value, CacheTime.ONE_DAY);
    }

    @Override
    public void logout(String token) {
        redissonService.removeString(CacheConstant.AUTH_TOKEN_CACHE_MAP, token);
    }

    @Override
    public void logout(HttpServletRequest request) {
        String token = request.getHeader(PubConstant.TOKEN_HEADER);
        redissonService.removeString(CacheConstant.AUTH_TOKEN_CACHE_MAP, token);
    }

    @Override
    public <T extends BaseUserInfo> T getUserInfo(String token) {
        return redissonService.getString(CacheConstant.AUTH_TOKEN_CACHE_MAP, token);
    }

    @Override
    public Collection<String> getWhiteList() {
        return Lists.newArrayList(
                "/api/auth/login",
                "/api/auth/captcha/**",
                "/*.html", //接口文档兼容
                "/*.js",   //接口文档兼容
                "/*.css",  //接口文档兼容
                "/favicon.ico");
    }

    @Override
    public boolean enableAuthentication() {
        return true;
    }
}
